package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    @Autowired
    private ApArticleMapper apArticleMapper;

    /**
     * 文章分页查询
     * @param tag
     * @param page
     * @param size
     * @return
     */
    @Override
    public ResponseResult homeList(Integer tag, Integer page, Integer size) {
        //1.创建分页对象
        IPage<ApArticle> iPage = new Page<ApArticle>(page,size);
        //2.创建条件封装对象，并封装查询条件
        QueryWrapper<ApArticle> queryWrapper = new QueryWrapper<>();
        //tag为标签信息，为0时表示推荐文章;is_delete=0 && is_down=0 表示未删除未下架的文章
        if (tag!=0){
            queryWrapper.eq("channel_id",tag);
        }
        queryWrapper.eq("is_delete",0);
        queryWrapper.eq("is_down",0);


        //3.执行查询，获取数据结果集
        IPage<ApArticle> apArticleIPage = apArticleMapper.selectPage(iPage, queryWrapper);
        return ResponseResult.okResult(apArticleIPage);
    }

    /**
     *设置静态页的url
     * @param staticUrl
     */
    @Override
    public void updateContentUrl(String staticUrl,Long id) {
        apArticleMapper.updateUrl(staticUrl,id);

    }
}
